package com.ojoin.trade.common.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImagesUtils {

    private static final String[] IMAGES_SUFFIXES = {
            "bmp", "jpg", "jpeg", "gif", "png", "tiff"
    };

    /**
     * 是否是图片附件
     *
     * @param filename
     * @return
     */
    public static boolean isImage(String filename) {
        if (filename == null || filename.trim().length() == 0) return false;
        return ArrayUtils.contains(IMAGES_SUFFIXES, FilenameUtils.getExtension(filename).toLowerCase());
    }
    
    /**
	 * Decode string to image
	 * @param imageString The string to decode
	 * @return decoded image
	 */
	public static BufferedImage decodeToImage(String imageString) {

		BufferedImage image = null;
		byte[] imageByte;
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			imageByte = decoder.decodeBuffer(imageString);
			ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
			image = ImageIO.read(bis);
			bis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return image;
	}

	/**
	 * Encode image to string
	 * @param image The image to encode
	 * @param type jpeg, bmp, ...
	 * @return encoded string
	 */
	public static String encodeToString(String  imageUrl, String type) {
		String imageString = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		try {
			URL url=new URL(imageUrl);
			ImageIO.write(ImageIO.read(url), type, bos);
			byte[] imageBytes = bos.toByteArray();

			BASE64Encoder encoder = new BASE64Encoder();
			imageString = encoder.encode(imageBytes);

			bos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageString;
	}
	
	public static byte[] encodeToBytes(String  imageUrl, String type) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] imageBytes = null;
		try {
			URL url=new URL(imageUrl);
			ImageIO.write(ImageIO.read(url), type, bos);
			imageBytes = bos.toByteArray();
			bos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageBytes;
	}
	
	/**
	 * save img to base64Img
	 * @param imageUrl
	 * @param type
	 * @return
	 */
	public static String encodeToBase64Image(String imageUrl,String type){
		return convertBase64Uri(encodeToString(imageUrl,type),type);
	}
	
	private static String convertBase64Uri(String encodedStr,String type){
		String prefix="data:image/"+type+";base64,";
		return prefix+encodedStr;
	}
	
	/**
	 * Encode image to string
	 * @param image The image to encode
	 * @param type jpeg, bmp, ...
	 * @return encoded string
	 */
	public static String encodeToString(BufferedImage image, String type) {
		String imageString = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		try {
			ImageIO.write(image, type, bos);
			byte[] imageBytes = bos.toByteArray();

			BASE64Encoder encoder = new BASE64Encoder();
			imageString = encoder.encode(imageBytes);

			bos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageString;
	}

	public static void main (String args[]) throws IOException {
		String imgUrl = "https://authcode.jd.com/verify/image?a=1&acid=c5a5deaa-fe5f-4a4d-99ca-539c8395faef&uid=c5a5deaa-fe5f-4a4d-99ca-539c8395faef&yys=1447987048179";
		BufferedImage newImg;
		String imgstr;
		imgstr = encodeToString(imgUrl, "png");
		System.out.println(imgstr);
		newImg = decodeToImage(imgstr);
		ImageIO.write(newImg, "png", new File("D:/CopyOfTestImage.png"));
	}

}
